package org.cbg.btn;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.List;

import javax.swing.SwingConstants;

import org.cbg.entity.Salegoods;
import org.cbg.panel.TraslationWantSendJishoushangpinJpanel;
import org.cbg.panel.TraslationWantSentjishoujinqianJpanel;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.TestPetJpanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.control.PetControl;
import org.come.entity.Goodstable;
import org.come.entity.RoleSummoning;
import org.come.model.Lingbao;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import com.tool.btn.MoBanBtn;
import com.tool.role.RoleData;
import com.tool.role.RoleLingFa;

public class CBGSellBtn extends MoBanBtn {

	private static final long serialVersionUID = 7278895297657916980L;
	private TraslationWantSendJishoushangpinJpanel jpanel1;
    private TraslationWantSentjishoujinqianJpanel jpanel2;

    /*
     * public CBGSellBtn(String iconpath, int type,String
     * text,TraslationWantSendJishoushangpinJpanel jpanel) { super(iconpath,
     * type); // TODO Auto-generated constructor stub this.setText(text);
     * setFont(UIUtils.TEXT_FONT1); setForeground(UIUtils.getColor("yellow"));
     * setVerticalTextPosition(SwingConstants.CENTER);
     * setHorizontalTextPosition(SwingConstants.CENTER); this.jpanel1=jpanel; }
     */
    public CBGSellBtn(String iconpath, int type, Color[] colors, Font font, String text,
            TraslationWantSendJishoushangpinJpanel jpanel) {
        super(iconpath, type, colors);
        // TODO Auto-generated constructor stub
        this.setText(text);
        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.jpanel1 = jpanel;
    }

    public CBGSellBtn(String iconpath, int type, Color[] colors, Font font, String text,
            TraslationWantSentjishoujinqianJpanel jpanel) {
        super(iconpath, type, colors);
        // TODO Auto-generated constructor stub
        this.setText(text);
        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.jpanel2 = jpanel;
    }



    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        // TODO Auto-generated method stub
        if (!Util.canBuyOrno) {
            ZhuFrame.getZhuJpanel().addPrompt2("背包没有解锁!");
            return;
        }
        if (jpanel1 != null) {
            jsWP();
        } else if (jpanel2 != null) {
            jsJB();
        }
    }

    /** 寄售面板 */
    public void jsWP() {
        Salegoods salegoods = jpanel1.getSalegoods();
        if (salegoods == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("先选择要上架的商品");
            return;
        }
        BigDecimal bigDecimal = jpanel1.getJG();
        if (bigDecimal == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("两个价格不一致");
            return;
        }
        if (bigDecimal.compareTo(new BigDecimal(5)) < 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("最少5元");
            return;
        }
        if (bigDecimal.compareTo(new BigDecimal(10000000)) > 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("最多1000万仙玉");
            return;
        }
        salegoods.setBuyrole(jpanel1.getZD());
        salegoods.setSaleprice(bigDecimal);
        
        long sx = bigDecimal.longValue() * 2 / 100; ;
        if ((sx) > RoleData.getRoleData().getLoginResult().getCodecard().longValue()) {
            ZhuFrame.getZhuJpanel().addPrompt2("你仙玉不够手续费");
            return;
        }

        if (salegoods.getSaletype() == 3 || salegoods.getSaletype() == 5) {// 物品
            Goodstable goodstable = GoodsListFromServerUntil.czGBG(salegoods.getOtherid());
            if (goodstable == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("所选物品不符合上架条件");
                return;
            }
            goodstable.setUsetime(0);
            GoodsListFromServerUntil.Deletebiaoid(goodstable.getRgid());
            String sendmes = Agreement.getAgreement().searchShelfGoodsAgreement(
                    GsonUtil.getGsonUtil().getgson().toJson(salegoods));
            SendMessageUntil.toServer(sendmes);
            jpanel1.thGood(null);
        } else if (salegoods.getSaletype() == 4) {// 召唤兽
            RoleSummoning pet = UserMessUntil.getPetRgid(salegoods.getOtherid());
         // 先判断召唤兽是否可交易
            if (pet == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("已不符合上架条件");
                return;
            } else if(pet.getQuality()!=null) {
            	if (!"2".equals(pet.getQuality())) {
            		ZhuFrame.getZhuJpanel().addPrompt("#R提示：#Y召唤兽#G【" + pet.getSummoningname() + "】#Y禁止交易！！！");
            		return;
            	}
            } else if (pet.getPetlock() == 1) {
                ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽已加锁");
                return;
            } else if (RoleData.getRoleData().getLoginResult().getSummoning_id() != null
                    && RoleData.getRoleData().getLoginResult().getSummoning_id().compareTo(pet.getSid()) == 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽已参战");
                return;
            }
            if (ZhuJpanel.getPetMount(pet.getSid()) != null) {
                ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽已管制");
                return;
            }
            List<RoleSummoning> pets = UserMessUntil.getPetListTable();
            pets.remove(pet);
            UserMessUntil.setPetListTable(PetControl.qc(pets));
            if (pet == UserMessUntil.getChosePetMes()) {
                UserMessUntil.setChosePetMes(null);
            }
            TestPetJpanel.showListModel(pets, RoleData.getRoleData().getLoginResult().getSummoning_id());
            jpanel1.qkPet(pet);
            String sendmes = Agreement.getAgreement().searchShelfGoodsAgreement(
                    GsonUtil.getGsonUtil().getgson().toJson(salegoods));
            SendMessageUntil.toServer(sendmes);
            jpanel1.thGood(null);
        } else if (salegoods.getSaletype() == 6) {// 灵宝
            Lingbao lingbao = RoleLingFa.getRoleLingFa().czGBG(salegoods.getOtherid());
            if (lingbao == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("已不符合上架条件");
                return;
            }
            RoleLingFa.getRoleLingFa().deleteling(lingbao);
            String sendmes = Agreement.getAgreement().searchShelfGoodsAgreement(
                    GsonUtil.getGsonUtil().getgson().toJson(salegoods));
            SendMessageUntil.toServer(sendmes);
            jpanel1.thGood(null);
        }
    }

    /** 寄售金币 */
    public void jsJB() {
        BigDecimal jg = jpanel2.getJG();
        BigDecimal jb = jpanel2.getJB();
        if (jg == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("两个价格不一致");
            return;
        }
        if (jg.compareTo(new BigDecimal(5)) < 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("最少5元");
            return;
        }
        if (jg.compareTo(new BigDecimal(10000000)) > 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("最多1000万仙玉");
            return;
        }
        if (jb == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("最少要卖1000万且是100万的整数倍");
            return;
        }
//        long sx = jg.longValue() * 2 / 100; ;
//        if ((sx) > RoleData.getRoleData().getLoginResult().getCodecard().longValue()) {
//            ZhuFrame.getZhuJpanel().addPrompt2("你仙玉不够手续费");
//            return;
//        }
        Salegoods salegoods = new Salegoods();
        salegoods.setSalename("大话币");
        salegoods.setSaletype(2);
        salegoods.setOtherid(jb);
        salegoods.setBuyrole(jpanel2.getZD());
        salegoods.setSaleprice(jg);
        String sendmes = Agreement.getAgreement().searchShelfGoodsAgreement(
                GsonUtil.getGsonUtil().getgson().toJson(salegoods));
        SendMessageUntil.toServer(sendmes);
        jpanel2.qk();
    }

}
